<?php
include_once '../meihe/common.php';
if ($islogin == 1) {
} else exit("<script language='javascript'>window.location.href='./login.php';</script>");
$title = '平台通知列表';
include_once './head.php';
$_GET['mod'] = $_GET['mod'] ?? '';
if ($_GET['mod'] == "add") {
?>
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header">
                    发布新通知
                </div>
                <div class="card-body">
                    <form class="form-horizontal layui-form">
                        <div class="form-group mb-3">
                            <label for="example-input-normal" style="font-weight: 500">接收用户类型：</label>
                            <select name="type" class="form-control" lay-filter="type">
                                <option value="0">全部用户</option>
                                <option value="1">所有普通用户</option>
                                <option value="2">所有贵族用户</option>
                            </select>
                        </div>
                        <div class="form-group mb-3">
                            <label for="example-input-normal" style="font-weight: 500">通知标题：</label>
                            <input type="text" name="title" class="form-control" lay-verType="tips" lay-verify="required">
                        </div>
                        <div class="form-group mb-3">
                            <label for="example-input-normal" style="font-weight: 500">通知内容：</label>
                            <textarea class="form-control" name="content" rows="8" style="width:100%;" lay-verType="tips" lay-verify="required"></textarea>
                        </div>
                        <button type="submit" class="btn btn-block btn-xs btn-outline-success" lay-submit lay-filter="submit_addmsg">发 布</button>
                    </form>
                    <br>
                    <a href="./msglist.php">>>返回通知列表</a>
                </div>
            </div>
        </div>
    </div>
    <?php
    include_once './bottom.php';
    ?>
    <script type="text/javascript">
        layui.use(['form'], function() {
            var form = layui.form;
            form.on('submit(submit_addmsg)', function(data) {
                layer.alert('是否要执行当前操作？', {
                    icon: 3,
                    btn: ['确定', '取消'],
                    btn1: function(layero, index) {
                        addmsg();
                    }
                });
                return false;
            });
            form.on('select(type)', function(data) {
                if (data.value >= '6') {
                    $("#frame_set1").show();
                } else {
                    $("#frame_set1").hide();
                }
            });
        });

        function addmsg() {
            var type = $("select[name='type']").val();
            var title = $("input[name='title']").val();
            var content = $("textarea[name='content']").val();
            var ii = layer.msg('正在发布中,请稍后...', {
                icon: 16,
                time: 10 * 1000
            });
            $.ajax({
                type: "POST",
                url: "ajax.php?act=addmsg",
                data: {
                    type: type,
                    title: title,
                    content: content
                },
                dataType: "json",
                success: function(data) {
                    layer.close(ii);
                    if (data.code == 0) {
                        layer.msg(data.msg, {
                            icon: 6,
                            end: function(layero, index) {
                                window.location.href = 'msglist.php';
                            }
                        });
                    } else {
                        layer.msg(data.msg, {
                            icon: 5
                        });
                    }
                },
                error: function(data) {
                    layer.msg('服务器错误', {
                        icon: 5
                    });
                    return false;
                }
            });
            return false;
        };
    </script>
<?php
} elseif ($_GET['mod'] == "edit") {
    $id = intval($_GET['id']);
    $row = $DB->get_row("SELECT * FROM meihe_message WHERE id='{$id}' limit 1");
?>
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header">
                    编辑通知
                </div>
                <div class="card-body">
                    <form class="form-horizontal layui-form">
                        <div class="form-group mb-3">
                            <label for="example-input-normal" style="font-weight: 500">接收用户类型：</label>
                            <select name="type" class="form-control" lay-filter="type">
                                <option <?php echo $row['type'] == 0 ? 'selected ' : '' ?>value="0">全部用户</option>
                                <option <?php echo $row['type'] == 1 ? 'selected ' : '' ?>value="1">所有普通用户</option>
                                <option <?php echo $row['type'] == 2 ? 'selected ' : '' ?>value="2">所有贵族用户</option>
                            </select>
                        </div>
                        <div class="form-group mb-3">
                            <label for="example-input-normal" style="font-weight: 500">通知标题：</label>
                            <input type="text" name="title" value="<?php echo $row['title'] ?>" class="form-control" lay-verType="tips" lay-verify="required">
                        </div>
                        <div class="form-group mb-3">
                            <label for="example-input-normal" style="font-weight: 500">通知内容：</label>
                            <textarea class="form-control" name="content" rows="8" style="width:100%;" lay-verType="tips" lay-verify="required"><?php echo $row['content'] ?></textarea>
                        </div>
                        <div class="form-group mb-3">
                            <label for="example-input-normal" style="font-weight: 500">是否显示：</label>
                            <select name="active" class="form-control" lay-filter="active">
                                <option <?php echo $row['active'] == 1 ? 'selected ' : '' ?>value="1">1_是</option>
                                <option <?php echo $row['active'] == 0 ? 'selected ' : '' ?>value="0">0_否</option>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-block btn-xs btn-outline-success" lay-submit lay-filter="submit_msgedit">修 改</button>
                    </form>
                    <br>
                    <a href="./msglist.php">>>返回通知列表</a>
                </div>
            </div>
        </div>
    </div>
    <?php
    include_once './bottom.php';
    ?>
    <script type="text/javascript">
        layui.use(['form'], function() {
            var form = layui.form;
            form.on('submit(submit_msgedit)', function(data) {
                layer.alert('是否要执行当前操作？', {
                    icon: 3,
                    btn: ['确定', '取消'],
                    btn1: function(layero, index) {
                        msgedit();
                    }
                });
                return false;
            });
            form.on('select(type)', function(data) {
                if (data.value >= '6') {
                    $("#frame_set1").show();
                } else {
                    $("#frame_set1").hide();
                }
            });
        });

        function msgedit() {
            var title = $("input[name='title']").val();
            var type = $("select[name='type']").val();
            var content = $("textarea[name='content']").val();
            var active = $("select[name='active']").val();
            var ii = layer.msg('正在修改中,请稍后...', {
                icon: 16,
                time: 10 * 1000
            });
            $.ajax({
                type: "POST",
                url: "ajax.php?act=msgedit&id=<?php echo $id; ?>",
                data: {
                    title: title,
                    type: type,
                    content: content,
                    active: active
                },
                dataType: "json",
                success: function(data) {
                    layer.close(ii);
                    if (data.code == 0) {
                        layer.msg(data.msg, {
                            icon: 6,
                            end: function(layero, index) {
                                window.location.reload();
                            }
                        });
                    } else {
                        layer.msg(data.msg, {
                            icon: 5
                        });
                    }
                },
                error: function(data) {
                    layer.msg('服务器错误', {
                        icon: 5
                    });
                    return false;
                }
            });
            return false;
        };
    </script>
<?php } else { ?>
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header">
                    平台通知列表&nbsp;&nbsp;&nbsp;<a href="javascript:searchClear()" class="badge badge-danger-info" title="刷新通知列表"><i class="layui-icon layui-icon-refresh"></i> 刷新</a>
                </div>
                <div class="card-body">
                    <div class="layui-elem-quote" id="blocktitle"></div>
                    <form onsubmit="return searchOrder()" method="GET" class="form layui-form">
                        <div class="form-group mb-3">
                            <select class="form-control" name="type">
                                <option value="0">全部</option>
                                <option value="1">通知标题</option>
                            </select>
                        </div>
                        <div class="form-group mb-3" id="searchword">
                            <input type="text" class="form-control" name="kw" placeholder="搜索内容" value="">
                        </div>
                        <div class="form-group mb-3">
                            <select class="form-control" name="method">
                                <option value="0">精确搜索</option>
                                <option value="1">模糊搜索</option>
                            </select>
                        </div>
                        <div class="form-group mb-3">
                            <button class="btn btn-outline-primary" type="submit"><i class="layui-icon layui-icon-search"></i> 搜索</button>&nbsp;
                            <a href="msglist.php?mod=add" class="btn btn-outline-secondary"><i class="layui-icon layui-icon-add-circle"></i> 发布新通知</a>
                        </div>
                    </form>
                    <div id="listTable"></div>
                </div>
            </div>
        </div>
    <?php
    include_once './bottom.php';
}
    ?>
    <script type="text/javascript">
        function listTable(query) {
            var url = window.document.location.href.toString();
            var queryString = url.split("?")[1];
            query = query || queryString;
            if (query == 'start' || query == undefined) {
                query = '';
                history.replaceState({}, null, './msglist.php');
            } else if (query != undefined) {
                history.replaceState({}, null, './msglist.php?' + query);
            }
            layer.closeAll();
            var ii = layer.msg('正在获取通知列表中,请稍后...', {
                icon: 16,
                time: 10 * 1000
            });
            $.ajax({
                type: 'GET',
                url: 'msglist-table.php?' + query,
                dataType: 'html',
                cache: false,
                success: function(data) {
                    layer.close(ii);
                    $("#listTable").html(data)
                },
                error: function(data) {
                    layer.msg('服务器错误', {
                        icon: 5
                    });
                    return false;
                }
            });
        }

        function searchOrder() {
            var type = $("select[name='type']").val();
            var kw = $("input[name='kw']").val();
            var method = $("select[name='method']").val();
            if (kw == '') {
                listTable('start');
            } else {
                listTable('type=' + type + '&kw=' + kw + '&method=' + method);
            }
            return false;
        }

        function searchClear() {
            $("select[name='type']").val(0);
            $("input[name='kw']").val('');
            $("select[name='method']").val(0);
            listTable('start');
        }

        function Active(id) {
            $.ajax({
                type: 'GET',
                url: 'ajax.php?act=msg_active&id=' + id,
                dataType: 'json',
                success: function(data) {},
                error: function(data) {
                    layer.msg('服务器错误', {
                        icon: 5
                    });
                    return false;
                }
            });
        }

        function show(id) {
            $.ajax({
                type: 'GET',
                url: 'ajax.php?act=getMessage&id=' + id,
                dataType: 'json',
                success: function(data) {
                    if (data.code == 0) {
                        layer.open({
                            type: 1,
                            skin: 'layui-layer-lan',
                            anim: 2,
                            shadeClose: true,
                            title: '查看平台通知',
                            content: '<div class="widget"><div class="widget-content widget-content-mini themed-background-muted text-center"><b>' + data.title + '</b><br/><small><font color="grey">管理员  ' + data.date + '</font></small></div><div class="widget-content">' + data.content + '</div></div>'
                        });
                    } else {
                        layer.msg(data.msg, {
                            icon: 5
                        });
                    }
                },
                error: function(data) {
                    layer.msg('服务器错误', {
                        icon: 5
                    });
                    return false;
                }
            });
        }

        function msgdel(id) {
            var confirmobj = layer.confirm('你确实要删除此通知吗？', {
                btn: ['确定', '取消']
            }, function() {
                $.ajax({
                    type: 'GET',
                    url: 'ajax.php?act=msgdel&id=' + id,
                    dataType: 'json',
                    success: function(data) {
                        if (data.code == 0) {
                            layer.msg(data.msg, {
                                icon: 6,
                                end: function(layero, index) {
                                    listTable();
                                }
                            });
                        } else {
                            layer.msg(data.msg, {
                                icon: 5
                            });
                        }
                    },
                    error: function(data) {
                        layer.msg('服务器错误', {
                            icon: 5
                        });
                        return false;
                    }
                });
            }, function() {
                layer.close(confirmobj);
            });
        }
        $(document).ready(function() {
            var items = $("select[default]");
            for (i = 0; i < items.length; i++) {
                $(items[i]).val($(items[i]).attr("default") || 0);
            }
            listTable();
        })
    </script>